package com.haizhi.crm.biz.task.message.handler;

import com.haizhi.crm.biz.task.message.model.ContextDto;
import com.haizhi.crm.common.constant.CrmSchema;
import com.haizhi.crm.persist.builder.PersistFactory;
import com.haizhi.crm.persist.builder.PersistReqBuilder;
import com.haizhi.crm.persist.builder.SourceBuilder;
import com.haizhi.crm.persist.constant.PersistTarget;
import com.haizhi.crm.persist.service.PersistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by wangshan on 2018/5/29.
 */
@Component
public class EsUpdateHandler extends AbstractHandler<ContextDto> {

    @Autowired
    private PersistService persistService;

    @Override
    protected MsgResponse process(ContextDto data) {
        // PersistReqBuilder
        PersistReqBuilder rb = PersistFactory.persistReqBuilder(CrmSchema.CRM, data.getMsgSchema());
        rb.addTargets(PersistTarget.ES);
        // SourceBuilder
        SourceBuilder sb = PersistFactory.sourceBuilder(data.getDetailKey());
        sb.updateNormalField("msg_generated", "true" );
        rb.addSource(sb);
        boolean success = persistService.bulkPersist(rb.get());

        return MsgResponse.success();
    }


}
